import React, { useEffect } from 'react';
import { PageHeader } from 'antd';
import styles from './index.less';
import Navigator from './navigator';
import Sidbar from './sidbar';
import { connect } from 'dva';
import './common.less';

const Layout = ({ children, dispatch, active, list = [] }) => {
  const { pathname, hash } = location;
  const activeArr = active.split('/');
  let title = '';
  if (activeArr.length < 3) {
    list.forEach(item => {
      if (item.key == activeArr[0]) title = item.name;
      if (item.children) {
        item.children.forEach(child => {
          if (child.key == activeArr[1]) title = child.name;
        });
      }
    });
  }
  if (['首页'].indexOf(title) >= 0) {
    title = null;
  }
  const checkAuth = () => {
    const token = sessionStorage.getItem('token');
    if (!token) {
      dispatch({
        type: 'auth/logout',
      });
    }
  };
  useEffect(() => {
    checkAuth();
    // 获取审批权限
    dispatch({
      type: 'base/approveSetting',
    });
    dispatch({
      type: '$router/save',
      payload: {
        active: location.pathname.slice(1),
      },
    });
  }, []);

  return (
    <div>
      {~hash.indexOf('login') ? (
        <div>{children}</div>
      ) : (
        <div>
          <Navigator></Navigator>
          <div className={styles.container}>
            <div className={styles.sidbar}>
              <Sidbar></Sidbar>
            </div>
            <div className={styles.content}>
              {title && (
                <PageHeader className="site-page-header" title={title} />
              )}

              {children}
            </div>
          </div>
        </div>
      )}
    </div>
  );
};
// conncet(state => state['auth'])(Layout);
export default connect(state => state['$router'])(Layout);
